<?php
namespace app\admin\model;

use think\Model;
/**
 * 
 */
class ManageMenu extends Model
{
	protected $table = 'super_manage_menu';
    protected $pk = 'id';
	protected function initialize()
    {
        //需要调用`Model`的`initialize`方法
        parent::initialize();;
        //TODO:自定义的初始化
    }
	// 实例化模型
	public function getManageMenu(){
		$db = new ManageMenu();
		$vn = $db->where(array('State' =>1 , 'Pid'=>0))
	    ->order('Sort', 'desc')
	    ->select();
	    foreach ($vn as $v => $n) {
	    	$vn[$v]['Submenu'] = getChildManageMenu($n['id']);
	    }
	    return $vn;
	}

	// 查询菜单
	public function getManageMenuTree(){
		$db = new ManageMenu();
		$vn = $db->select();
	    $data = getCategoryInfinite($vn);
	    return $data;
	}
	// 删除菜单
	public function deleteManageMenu($id){
		$db = new ManageMenu();
		if($db->where(array('pid'=>$id))->find())
		return json(['code'=>0,'msg'=>'存在子菜单无法删除']);
		if($db->where(array('id'=>$id))->delete())
		return json(['code'=>1,'msg'=>'删除成功']);
		return json(['code'=>0,'msg'=>'删除失败']);
	    
	}
	// 审核菜单
	public function toexamineManageMenu($id,$sta){
		$db = new ManageMenu();
		$sta = $sta == 1?2:1;
		if($db->where(array('id'=>$id))->setField('State',$sta))
		return json(['code'=>1,'msg'=>'更新成功']);
		return json(['code'=>0,'msg'=>'更新失败']);
	    
	}
	// 修改菜单
	public function editManageMenuInfo($id,$nme,$sto,$sup,$ctrl,$alt){
		$db = new ManageMenu();
		$sto = $sto?$sto:0;
		if($sup == $id)
		return json(['code'=>0,'msg'=>'修改失败']);
		if($db->where(array('id'=>$id))->update(array('Name'=>$nme,'Sort'=>$sto,'Pid'=>$sup,'Ctrl'=>$ctrl,'Alt'=>$alt)))
		return json(['code'=>1,'msg'=>'修改成功']);
		return json(['code'=>0,'msg'=>'修改失败']);
	    
	}
	//添加菜单
	public function addManageMenuInfo($data)
	{
		$db = new ManageMenu();
		if($db->field('Name,Ctrl,Alt,Pid')->insert($data))
		return json(['code'=>1,'msg'=>'添加成功']);
		return json(['code'=>0,'msg'=>'添加失败']);
	}
}